

const $redis = require('./redis.js')
const $axios =require('./axios.js'); 
const weixin = {
    // appid: "wx396ef9d7f555f752",          //  
    // appsecret: '65178767ff15686ebdb7746a9bfbc2dc',   // 
    appid: "wxe674a904b7fd57b8",          //  
    appsecret: 'b7123f68e2a14116f4beecf8d2c99835',   // 
    async getToken(){
         let data = await  $redis.get("access_token_"+weixin.appid);
         let access_token ="";
         let time = Math.floor(Date.now() / 1000);
         try {
            data = JSON.parse(data);
            if(data.expire > time){
                access_token = data.access_token;
            }
         } catch (error) {
             
         }
        //  console.log("data:",data)
        
         if(access_token){
             return access_token;
         }
         let url ='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+weixin.appid+'&secret='+weixin.appsecret;
         let  res = await  $axios.get(url);
         if(res.data.errcode>0){
            res.code = res.data.errcode;
            res.msg = res.data.errmsg
        }
        // console.log("getToken res:",res);
         if(res.code==0 && res.data.access_token){
            access_token = res.data.access_token;
             let data= {
                access_token:access_token,
                expire:time+1800
             };
            
             $redis.set("access_token_"+weixin.appid,JSON.stringify(data));
            //  $redis.expire("access_token_"+weixin.appid,1800)
         }else{
             console.log("getToken error:",res);
         }
         return access_token;
    },

    async code2Session(code){
        let url ='https://api.weixin.qq.com/sns/jscode2session?appid='+weixin.appid+'&secret='+weixin.appsecret+'&js_code='+code+'&grant_type=authorization_code';
        let  res = await  $axios.get(url);
        if(res.data.errcode>0){
            res.code = res.data.errcode;
            res.msg = res.data.errmsg
        }
        return res;
    },
    async getuserphonenumber(code){
        let access_token = await weixin.getToken();
        let url ='https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='+access_token;
        let  res = await  $axios.post(url,{
            code:code
        });
        if(res.data.errcode>0){
            res.code = res.data.errcode;
            res.msg = res.data.errmsg
        }
        return res;
    },
    // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
    // {
    //     "openid": "OPENID",
    //     "scene": 1,
    //     "version": 2,
    //     "content":"hello world!"
    // }
 
    async msg_sec_check(params={}){
        let access_token = await weixin.getToken();
        let url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token='+access_token;
        params.version =2;
        params.scene =2;
        let  res = await  $axios.post(url,params);
        if(res.data.errcode>0){
            res.code = res.data.errcode;
            res.msg = res.data.errmsg
        }
        if(res.data.result && res.data.result.suggest){
            res.data.suggest_type = res.data.result.suggest;
        }
        return res;
    },
    


    
}
module.exports = weixin
